# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  https://gitee.com/autumner/pythoncookbook
@software: PyCharm 
@file: shell_argparse.py 
@time: 2019/7/31 下午4:09
@note:
argparse 模块可被用来解析命令行选项
"""
import argparse

parser = argparse.ArgumentParser(description='Search some files')
parser.add_argument(dest='filenames', metavar='filename', nargs='*')
parser.add_argument('-p', '--pat', metavar='pattern', required=True, dest='patterns',
                    action='append',
                    help='text pattern to search for')
parser.add_argument('-v', dest='verbose', action='store_true',
                    help='verbose mode')
parser.add_argument('-o', dest='outfile', action='store',
                    help='output file')
parser.add_argument('--speed', dest='speed', action='store',
                    choices={'slow', 'fast'}, default='slow',
                    help='search speed')
"""
dest 参数指定解析结果被指派给属性的名字。 
metavar 参数被用来生成帮助信息。 
action 参数指定跟属性对应的处理逻辑，通常的值为 store , 被用来存储某个值或讲多个参数
值收集到一个列表中。
"""
args = parser.parse_args()

print(f"filenames= {args.filenames}")
print(f"patterns= {args.patterns}")
print(f"verbose= {args.verbose}")
print(f"outfile= {args.outfile}")
print(f"speed= {args.speed}")


"""
Example usage
use Terminal:
1.python shell_argparse.py -h
2.python shell_argparse.py -v -p spam --pat=eggs foo.txt bar.txt
3.python shell_argparse.py -v -p spam --pat=eggs foo.txt bar.txt -o results
4.python shell_argparse.py -v -p spam --pat=eggs foo.txt bar.txt -o results --speed=fast

"""